
package com.wwy.monitor.service.impl;

import com.wwy.monitor.common.MessageException;
import com.wwy.monitor.mapper.tddl.MonitorAppMapper;
import com.wwy.monitor.model.enumeration.AppStatusEnum;
import com.wwy.monitor.model.enumeration.EnableStatusEnum;
import com.wwy.monitor.model.tddl.dto.MonitorAppDTO;
import com.wwy.monitor.model.tddl.query.MonitorAppQuery;
import com.wwy.monitor.service.MonitorAppService;
import com.wwy.monitor.service.MonitorConfigDetailService;
import com.wwy.monitor.service.MonitorIpService;
import com.wwy.monitor.service.MonitorTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author tianquan.ld
 * @version V1.0
 * <p>Description: çæ§app service impl</p>
 * @date 2019-10-30 21:18
 */
@Service(value = "monitorAppService")
public class MonitorAppServiceImpl implements MonitorAppService {
    @Resource
    private MonitorAppMapper monitorAppMapper;
    @Resource(name = "monitorIpService")
    private MonitorIpService monitorIpService;
    @Resource(name = "monitorConfigDetailService")
    private MonitorConfigDetailService monitorConfigDetailService;
    @Resource(name = "monitorTaskService")
    private MonitorTaskService monitorTaskService;

    @Override
    public boolean insert(MonitorAppDTO entity) {
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        return monitorAppMapper.insert(entity) == 1;
    }

    @Override
    public boolean deleteById(Long id) {
        int count = monitorAppMapper.deleteById(id);
//åºç¨ä¸çº¿,é»è¾å é¤ææçæºå¨
        monitorIpService.deleteByAppId(id);
//é»è¾å é¤ææçéç½®è¯¦æ
        monitorConfigDetailService.deleteByAppId(id);
//åæ­¢æ§è¡ä¸­çå®æ¶ä»»å¡
        monitorTaskService.stopTaskByAppId(id);
        return count == 1;
    }

    @Override
    public boolean updateById(MonitorAppDTO entity) {
        entity.setUpdateTime(new Date());
        return monitorAppMapper.updateById(entity) == 1;
    }

    @Override
    public boolean updateStatusById(MonitorAppDTO param) {
        if (null == param.getId()) {
            throw new MessageException("idä¸è½ä¸ºç©º");
        }
        if (null == param.getStatus()) {
            throw new MessageException("ç¶æä¸è½ä¸ºç©º");
        }
        if (param.getStatus() == AppStatusEnum.OFFLINE.getValue()) {
//ä¸çº¿æææºå¨
            monitorIpService.offlineIpByAppId(param.getId());
//ä¸çº¿ææéç½®è¯¦æ
            monitorConfigDetailService.offlineConfigDetailByAppId(param.getId());
//åæ­¢æ§è¡ä¸­çå®æ¶ä»»å¡
            monitorTaskService.stopTaskByAppId(param.getId());
        }
        return monitorAppMapper.updateStatusById(param) == 1;
    }

    @Override
    public List<MonitorAppDTO> selectByQuery(MonitorAppQuery query) {
//query.setOrderBy(" update_time desc ");
        return monitorAppMapper.selectByQuery(query);
    }

    @Override
    public MonitorAppDTO selectById(Long id) {
        return monitorAppMapper.selectById(id);
    }

    @Override
    public Integer count(MonitorAppQuery query) {
        return monitorAppMapper.count(query);
    }

    @Override
    public void checkApp(MonitorAppDTO dto) {
        MonitorAppDTO result = monitorAppMapper.selectByAoneAppId(dto);
        if (result != null) {
            if (dto.getId() == null) {
                throw new MessageException("è¯¥aoneIdå·²å­å¨");
            }
            if (!dto.getId().equals(result.getId())) {
                throw new MessageException("è¯¥aoneIdå·²å­å¨");
            }
        }
        result = monitorAppMapper.selectByAppName(dto);
        if (null != result) {
            if (dto.getId() == null) {
                throw new MessageException("åºç¨åå·²å­å¨");
            }
            if (!dto.getId().equals(result.getId())) {
                throw new MessageException("åºç¨åå·²å­å¨");
            }
        }
    }
}